﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DevExpress.Web.Mvc;
using System.Web.UI.WebControls;
using Services.Objetos;
using Providers.Objetos;
using WebMvc.Models;
using System.ComponentModel.DataAnnotations;

namespace WebMvc.Controllers
{
    public class ObjetosController : Controller
    {
        IService _service;

        public ObjetosController()
        {
            IProvider provider = new Provider();
            this._service = new Service(provider);
        }

        //
        // GET: /Customer/

        public ViewResult Index()
        {
            return View(GetViewModels());
        }

        //
        // GET: /Customer/Details/5

        public ViewResult Details(Guid id)
        {
            Objeto item = _service.Get(id);
            return View(GetViewModel(item));
        }

        //
        // GET: /Customer/Create

        public ActionResult Create()
        {
            InitializeViewBag();

            return View();
        }

        private void InitializeViewBag()
        {
        }

        //
        // POST: /Customer/Create

        [HttpPost]
        public ActionResult Create(Objeto item)
        {
            if (!ModelState.IsValid)
            {
                InitializeViewBag();
                return View(GetViewModel(item));
            }
            item.Guid = Guid.NewGuid();
            _service.Insert(item);

            return RedirectToAction("Index");
        }

        //
        // GET: /Customer/Edit/5

        public ActionResult Edit(Guid id)
        {
            Objeto item = _service.Get(id);

            InitializeViewBag();
            return View(GetViewModel(item));
        }

        //
        // POST: /Customer/Edit/5

        [HttpPost]
        public ActionResult Edit(Objeto item)
        {
            if (!ModelState.IsValid)
            {
                InitializeViewBag();
                return View(GetViewModel(item));
            }

            _service.Update(item);

            return RedirectToAction("Index");
        }

        //
        // POST: /Customer/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult Delete(Guid id)
        {
            _service.Delete(id);

            return RedirectToAction("Index");
        }

        public ActionResult ObjetosPartial()
        {
            return PartialView(GetViewModels());
        }

        [HttpPost]
        public ActionResult ExportToPdf()
        {
            return GridViewExtension.ExportToPdf(GridViewSettings, GetViewModels());
        }

        private IEnumerable<ObjetoViewModel> GetViewModels()
        {
            return _service.Get().Select(item =>
            {
                return GetViewModel(item);
            });
        }

        private static ObjetoViewModel GetViewModel(Objeto item)
        {
            return new ObjetoViewModel
            {
                Guid = item.Guid,
                Name = item.Name,
                Details = item.Details
            };
        }


        #region Grid

        public static GridViewSettings GridViewSettings
        {
            get
            {
                return CreateExportGridViewSettings();
            }
        }

        private static GridViewSettings CreateExportGridViewSettings()
        {
            GridViewSettings settings = new GridViewSettings();

            settings.Name = "gvObjetos";
            settings.CallbackRouteValues = new { Controller = "Objeto", Action = "ObjetosPartial" };
            settings.ClientSideEvents.EndCallback = "gridColumnSorting";
            settings.Width = Unit.Percentage(100);
            settings.Settings.ShowFilterRow = true;
            settings.Columns.Add("Guid", "Código").Width = Unit.Percentage(10);// 130;
            settings.Settings.ShowFilterRow = true;
            settings.Columns.Add("Name", "Nombre").Width = Unit.Percentage(10);// 130;
            settings.Columns.Add("Details", "Observaciones").Width = Unit.Percentage(40);// 130;

            settings.Settings.ShowVerticalScrollBar = false;
            settings.Settings.ShowHorizontalScrollBar = false;
            settings.Settings.VerticalScrollableHeight = 200;
            settings.SettingsPager.PageSize = 50;
            settings.Height = Unit.Percentage(100);

            return settings;
        }
    }

        #endregion
}